package com.data.modules.system.util.crypto;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

public class Decrypt {

    //public static void main(String[] args) throws Exception {
    //    JSONObject responseStr = JSON.parseObject("{\"challenge\":\"fe34e8d1-fe16-47af-b6f5-de153cb31300\",\"type\":\"url_verification\",\"token\":\"7FbbCl1dWKaT3NE4ABnTXvGpuVqyOTka\"}");
    //    String key = String.valueOf(responseStr.get("challenge"));
    //    Decrypt d = new Decrypt("Xam3jN4pCu92R3sU35r2EcW3V3xCffxt");
    //    System.out.println(d.decrypt(key)); //hello world
    //}


    public static void main(String[] args) throws Exception {
        //事件回调模拟参数
        JSONObject responseStr = JSON.parseObject("{\"encrypt\":\"C+ZvLFdPgtaEn4NX0lc17VOqB9pTxZNyB8CpjMMO0HQ27Ty2TZ12n/HG7E/lyhkcnDwdnjy84hDJYAQV3ICzusgPumnViT/foC1G53lW5KNMzA0sr+/z+lrUn3mBbF+7xwpkx4jxnyXOJbGDy3HPJAGK8+UzUKOfEj96KKlqFrlz18wkjQGmb65F2rcbekzm9kiOnlOjGO8Jl+cz7MRyS6Ds/Ynynw49L6vkG0uYGu2ykpLfj3xTcvgcdMA59Fif5fBsn4H64FOkPBjHXD1BbiAJOdLQ4LVsezmWRMu0nHrjMnF5UgwpjpN0PuSH2uhEFImOzdy86ZxpPgYng55YQjntOHaa0lDk8r/Us42MmyFoBsOzNAvi6pYk4NHTPH3NSCIHk38lxNTPWuGaBMUDRIMewlkiLN0e3W1fqd/T80uuXblRoUuQ5rWdof9KJGto8ryO+YFhKU/vAjZQeq9jSVXUs0VLMwuLEq7MMwmk+9mPoO4MjaISilCl0NuiEtbfAnRMKVF3nItyI9M4Kd/fn/9Csju8wekh+6HUgA3a5/Piu6RKIZAcAwIEC7zQvpHiPBEQqa7Rm0IyUwzDMEkNETuR+KvqNOsltL4pmrmmYUejY6nt4iRjSvkx5cwj09md1h9cwTcJl8dpADmzZHAU9GlN+yU8YN9rILgV8g2Jy0G5hcrloH6HSM/+kH8/p+cZosB3X53UGIpv1h3iAwSAbQ==\"}");
        Decrypt d = new Decrypt("Xam3jN4pCu92R3sU35r2EcW3V3xCffxt");
        System.out.println(d.decrypt( responseStr.get("encrypt").toString())); //hello world
    }

    private byte[] keyBs;

    public Decrypt(String key) {
        MessageDigest digest = null;
        try {
            digest = MessageDigest.getInstance("SHA-256");
        } catch (NoSuchAlgorithmException e) {
            // won't happen
        }
        keyBs = digest.digest(key.getBytes(StandardCharsets.UTF_8));
    }

    public String decrypt(String base64) throws Exception {
        byte[] decode = Base64.getDecoder().decode(base64);
        Cipher cipher = Cipher.getInstance("AES/CBC/NOPADDING");
        byte[] iv = new byte[16];
        System.arraycopy(decode, 0, iv, 0, 16);
        byte[] data = new byte[decode.length - 16];
        System.arraycopy(decode, 16, data, 0, data.length);
        cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(keyBs, "AES"), new IvParameterSpec(iv));
        byte[] r = cipher.doFinal(data);
        if (r.length > 0) {
            int p = r.length - 1;
            for (; p >= 0 && r[p] <= 16; p--) {
            }
            if (p != r.length - 1) {
                byte[] rr = new byte[p + 1];
                System.arraycopy(r, 0, rr, 0, p + 1);
                r = rr;
            }
        }
        return new String(r, StandardCharsets.UTF_8);
    }
}